#ifndef INTERFACES_H
#define INTERFACES_H

/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool. 

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.
  
*/

#include <string>
#include <utility>

#include <boost/shared_ptr.hpp>

#include "enumerations.h"
#include "CVector3.h"
#include "model_obj.h"

using namespace std;

// interface list

class IEvent;
class IEventListener;
class IResHandle;
class IResource;
class ICollisionShape;
class IObject;
class ICamera;
class ILight;
class IOGLObject;
class IPhysicsSystem;

// to be defined afterwards
class CMaterial;
class CMeshInfo;

// global typedefs

typedef boost::shared_ptr< IEvent > EventPtr;
typedef boost::shared_ptr< IEventListener > EventListenerPtr;
typedef boost::shared_ptr< IResHandle > ResHandlePtr;
typedef boost::shared_ptr< IOGLObject > GLObjectPtr;
typedef boost::shared_ptr< ILight > GLLightPtr;
typedef boost::shared_ptr< ICamera > GLCameraPtr;
typedef boost::shared_ptr< ICollisionShape > CollisionShapePtr;
typedef boost::shared_ptr< IObject > ObjectPtr;
typedef boost::shared_ptr< IPhysicsSystem > PhysicsSysPtr;

typedef std::pair< CollisionShapePtr, CollisionShapePtr > CollisionPair;

// functions

void SeparateObjects(IObject *obj1, IObject *obj2);

// interfaces for all game subsystems and objects

// Events

class IEvent
{
public:
    virtual ~IEvent() {}
    virtual eEventType getEventType() = 0; //returns the event type ID
    virtual string getEventTypeStr() = 0; //returns a human-readable string
    //with the event type ID name. mainly for debug purposes.
};

class IEventListener
{
public:
    virtual ~IEventListener() {}
    virtual string const getName() = 0;
    virtual bool processEvent(EventPtr event) = 0;
};

// Resources

class IResHandle
{
public:
	virtual ~IResHandle() {}
	virtual eResourceType getType() = 0;
};

class IResource
{
public:
	virtual ~IResource() {}
	virtual eResourceType getType() = 0;
	virtual ResHandlePtr getHandle() = 0;
	virtual bool load(string resName) = 0;
	virtual bool unload() = 0;
};

// objects

// collision shape

class ICollisionShape
{
public:
	virtual ~ICollisionShape() {}
	virtual eCollisionShape GetType() = 0;
	virtual void SetParent(IObject* parent) = 0;
	virtual IObject* GetParent() = 0;
	// Returning a collision pair is particularly useful for composite shapes, made up by a number
	// of basic ones (i.e. a cube is made up by 6 planes). Therefore, if a collision has happened,
	// we will know exactly in which part of the complex shape.
	virtual CollisionPair TestCollision(CollisionShapePtr  other, bool& collided) = 0;
};

// object
class IObject
{
public:
    virtual ~IObject() {}

    // identifiers getters & setters
    // getters return a constant reference
    virtual eObjectType& GetObjectType() = 0;
    virtual eObjectID& GetObjectID() = 0;
    virtual string& GetName() = 0;
    virtual void SetObjectType(eObjectType type) = 0;
    virtual void SetObjectID(eObjectID id) = 0;
    virtual void SetName(string name) = 0;

    // Physical porperties getters & setters
    // getters return a constant reference
    virtual float& GetMass() = 0;
    virtual float& GetDragCoeff() = 0;
    virtual float& GetCrossSection() = 0;
    virtual void SetMass(float mass) = 0;
    virtual void SetDragCoeff(float cd) = 0;
    virtual void SetCrossSection(float Ae) = 0;

    //Position and orientation getters & setters
    // getters return a modifiable reference
    virtual CVector3& GetPosition() = 0;
    virtual CVector3& GetVelocity() = 0;
    virtual CVector3& GetHeading() = 0;
    virtual CVector3& GetRotationAxis() = 0;
    virtual float& GetRotationAngle() = 0;

    virtual void SetPosition(CVector3& pos) = 0;
    virtual void SetVelocity(CVector3& vel) = 0;
    virtual void SetHeading(CVector3& heading) = 0;
    virtual void SetRotationAxis(CVector3& axis) = 0;
    virtual void SetRotationAngle(float angle) = 0;

    // Collision shape and MeshInfo
    virtual void SetCollisionShape(CollisionShapePtr cs) = 0;
    virtual void SetMeshInfo(CMeshInfo& meshInfo) = 0;
    virtual CollisionShapePtr GetCollisionShape() = 0;
    virtual CMeshInfo& GetMeshInfo() = 0;


    // State flags getters and setters
    virtual bool IsActive() = 0;
    virtual void SetActive(bool active) = 0;
    virtual bool IsFalling() = 0;
    virtual void SetFalling(bool falling) = 0;

    virtual bool IsVisible() = 0;
    virtual void SetVisible(bool visible) = 0;

    // Physics system interface
    virtual void Move(IPhysicsSystem* physics) = 0;
    virtual CollisionPair TestCollision(CollisionShapePtr other, bool& collided) = 0;
};

// light
class ILight
{
public:

	virtual ~ILight(){}
	virtual void SetName(string &name) = 0;
	virtual void SetAmbientColor(float red, float green , float blue, float alpha) = 0;
	virtual void SetDiffuseColor(float red, float green , float blue, float alpha) = 0;
	virtual void SetSpecularColor(float red, float green , float blue, float alpha) = 0;
	virtual void SetPosition(CVector3 pos) = 0;
	virtual string GetName() = 0;
	virtual float* GetPosition() = 0; // easier to use in the gl engine
	virtual float* GetAmbientColor() = 0;
	virtual float* GetDiffuseColor() = 0;
	virtual float* GetSpecularColor() = 0;
};

// camera

class ICamera
{
public:
    virtual ~ICamera() {}
    virtual void SetName(string &name) = 0;
    virtual void SetPosition(CVector3 position) = 0;
    virtual void SetLookAt(CVector3 direction) = 0;
    virtual void SetUpVector(CVector3 up) = 0;
    virtual void Pitch(float angle) = 0; //angle in degrees
    virtual void Yaw(float angle) = 0; //angle in degrees
    virtual void Roll(float angle) = 0; //angle in degrees
    virtual void SetClipDistanceNear(float nearPlane) = 0;
    virtual void SetClipDistanceFar(float farPlane) = 0;
    virtual string GetName() = 0;
    virtual CVector3 GetPosition() = 0;
    virtual CVector3 GetLookAt() = 0;
    virtual CVector3 GetUpVector() = 0;
    virtual float GetClipDistanceNear() = 0;
    virtual float GetClipDistanceFar() = 0;
};

// graphic objects

class IOGLObject
{
public:

    virtual ~IOGLObject() {}
    virtual eObjectType & Type() = 0;
    virtual eObjectID & Id() = 0;
    virtual string & Name() = 0; //object name
    virtual string & ModelFileName() = 0; // name of the .obj file without path
    virtual string & ColorMapName() = 0; //name of the color texture map (if any)
    virtual CMaterial & Material() = 0; //material properties
    virtual CVector3 & Position() = 0; //world position
    virtual CVector3 & Heading() = 0;
    virtual CVector3 & RotationAxis() = 0;
    virtual float & RotationAngle() = 0;
    virtual float & Offset() = 0;
    virtual void SetModelPtr(const ModelOBJ* modelref) = 0;
    virtual const ModelOBJ* ModelPtr() = 0; //3d .obj model (if any)
    virtual bool & HasModel() = 0; //object has a 3d model attached to it.
    virtual bool & IsVisible() = 0; //object will be drawn if true
    virtual void BasicDraw() = 0; //draws the object as a simple primitive or a collection of simple primitives.
};

// physics subsystem

class IPhysicsSystem
{
public:
    virtual ~IPhysicsSystem() {}

    /*This is the state propagation function. takes a list of entities present in the world
    that can be moved (i.e. the spheres and not the ground) and computes the new positions
    and velocites after a fixed time interval.*/
    virtual void MoveObject(IObject *object) = 0;

    //Evaluates the state derivatives (i.e. velicity and acceleration).
    virtual CVector3 Derivatives(CVector3& pos, CVector3& vel, float mass, float CD, float Ae, bool falling) = 0;

    /*This function is responsible to properly update the entities velocities,
    if a collision is detected*/
    virtual void ElasticCollision(CollisionPair &c_pair) = 0;

    //Allows to reset the environment variables.
    virtual void ResetEnvironmentVariables(float newTimeStep, CVector3 gravity, float friction, float density ) = 0;

};

#endif // INTERFACES_H
